package org.wedding.center.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import org.wedding.center.dto.query.NoticeQuery;
import org.wedding.center.entity.NoticeInfo;
import org.wedding.center.mapper.NoticeInfoMapper;
import org.wedding.center.service.NoticeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.system.entity.UserInfo;
import org.wedding.system.mapper.UserMapper;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.dto.PageModel;
import org.wedding.utils.PageUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 通知信息表(NoticeInfo)表服务实现类
 *
 * @author songkaiyue
 * @since 2025-05-19 15:34:13
 */
@Service
public class NoticeInfoServiceImpl extends ServiceImpl<NoticeInfoMapper, NoticeInfo> implements NoticeInfoService {
    @Resource
    private UserMapper userMapper;

    /**
     * 分页查询所有数据
     *
     * @return
     */
    @Override
    public CommonResult<PageModel> findAll(NoticeQuery query) {
        try {
            String role = StpUtil.getSession().get("role").toString();
            if ("0".equals(role) || "1".equals(role)) {
                query.setUserId(StpUtil.getLoginIdAsString());
            }
            List<NoticeInfo> list = baseMapper.selectAll(query);
            List<NoticeInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }

    }

    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<NoticeInfo> view(String id) {
        try {
            NoticeInfo noticeInfo = baseMapper.findById(id);
            return CommonResult.success(noticeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param noticeInfo
     * @return
     */
    @Override
    public CommonResult add(NoticeInfo noticeInfo) {
        try {
            String userId = noticeInfo.getUserId();
            if (ObjectUtil.isEmpty(userId)) {
                userId = StpUtil.getSession().get("userId").toString();
            }
            noticeInfo.setCreator(userId);
            noticeInfo.setId(IdUtil.simpleUUID());
            noticeInfo.setCreateTime(DateUtil.now());
            noticeInfo.setStatus("0");
            noticeInfo.setDelFlag("0");
            if (StrUtil.isEmpty(noticeInfo.getUserName()) && StrUtil.isNotEmpty(noticeInfo.getUserId())) {
                UserInfo userInfo = userMapper.findById(noticeInfo.getUserId());
                if (ObjectUtil.isNotEmpty(userInfo)) {
                    noticeInfo.setUserName(userInfo.getUserName());
                }
            }
            if (ObjectUtil.isEmpty(noticeInfo.getType())) {
                noticeInfo.setType("0");
            }
            if (ObjectUtil.isEmpty(noticeInfo.getQuality())) {
                noticeInfo.setQuality("low");
            }
            baseMapper.insert(noticeInfo);
            return CommonResult.success(noticeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param noticeInfo
     * @return
     */
    @Override
    public CommonResult updateEntity(NoticeInfo noticeInfo) {
        try {
            noticeInfo.setUpdateTime(DateUtil.now());
            baseMapper.updateById(noticeInfo);
            return CommonResult.success(noticeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

}
